package com.jspgou.cms.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.springframework.stereotype.Repository;

import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.StoreCategoryDao;
import com.jspgou.cms.entity.StoreCategory;

@Repository
public class StoreCategoryDaoImpl extends HibernateBaseDao<StoreCategory, Long> implements StoreCategoryDao {
	public Pagination getPage(int pageNo, int pageSize) {
		Criteria crit = createCriteria();
		Pagination page = findByCriteria(crit, pageNo, pageSize);
		return page;
	}
	
	@SuppressWarnings("unchecked")
	public List<StoreCategory> getList(Long webId,Long parentId,Long storeId){
		Finder f=Finder.create("from StoreCategory bean where 1=1");
		if(webId!=null){
			f.append(" and bean.website.id=:webId");
			f.setParam("webId",webId);
		}
		if(parentId==null){
			f.append(" and bean.parent.id is null");
		}else{
			f.append(" and bean.parent.id=:parentId");
			f.setParam("parentId",parentId);
		}
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId",storeId);
		}
		return find(f);
	}

	public StoreCategory findById(Long id) {
		StoreCategory entity = get(id);
		return entity;
	}

	public StoreCategory save(StoreCategory bean) {
		getSession().save(bean);
		return bean;
	}

	public StoreCategory deleteById(Long id) {
		StoreCategory entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	
	@Override
	protected Class<StoreCategory> getEntityClass() {
		return StoreCategory.class;
	}
}